package biostienda.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import biostienda.excepcion.DAOExcepcion;

import biostienda.negocio.GestionPublicacion;

/**
 * Servlet implementation class RegistroPublicacionServlet
 */
@WebServlet("/RegistroPublicacionServlet")
public class RegistroPublicacionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistroPublicacionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String nom = request.getParameter("publicacion");
		String tip = request.getParameter("tipocategoria");
		String usu = request.getParameter("tipousuario");
		String des = request.getParameter("descripcion");
		String est = request.getParameter("estado");
		String can = request.getParameter("cantidad");
		String pre = request.getParameter("precio");
		String fot = request.getParameter("foto");
		String dur = request.getParameter("duracion");
		String fec = request.getParameter("fecpublic");
		
		int pub=2;
		int cat= Integer.parseInt(tip);
		int precio= Integer.parseInt(pre);
		int cantidad= Integer.parseInt(can);
		int usua= Integer.parseInt(usu);
		
		SimpleDateFormat parse = new SimpleDateFormat("dd-MM-yyyy");

		String strfecha = fec;
		java.util.Date datefecha = null;
		try {
			datefecha = parse.parse(strfecha);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		java.sql.Date fecha = new java.sql.Date(datefecha.getTime());

		
		
		GestionPublicacion negocio= new GestionPublicacion();
	
		try {
            negocio.insertar( nom, des, est ,cantidad,precio,fecha,fot,dur, usua,cat,pub );
            response.sendRedirect(request.getContextPath()  + "/registrovalido.jsp");
            
    } catch (DAOExcepcion q) {

		RequestDispatcher rd = request.getRequestDispatcher("registropublicacion.jsp");
		rd.forward(request, response);
}

		
		
	}

}
